package com.admin.service.impl;

import com.admin.common.enums.EnumIstatus;
import com.admin.common.exception.BizException;
import com.admin.domain.*;
import com.admin.mapper.*;
import com.admin.service.*;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Slf4j
@Service
public class DeclareUserServiceImpl implements DeclareUserService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private DeclareService declareService;
    @Autowired
    private SysParamService sysParamService;
    @Autowired
    private DeclareMapper declareMapper;
    @Autowired
    private DeclareBasicMapper declareBasicMapper;
    @Autowired
    private DeclareInstitutionMapper institutionMapper;
    @Autowired
    private DeclareDisasterPlanMapper disasterPlanMapper;
    @Autowired
    private DeclareDisasterFacilitiesMapper disasterFacilitiesMapper;
    @Autowired
    private DeclareScienceMapper declareScienceMapper;
    @Autowired
    private DeclareClassicCaseMapper classicCaseMapper;
    @Autowired
    private DeclareWeatherBureauMapper weatherBureauMapper;
    @Autowired
    private DeclareUpgradeMapper upgradeMapper;
    @Autowired
    private AttachmentMapper attachmentMapper;
    @Autowired
    private DeclareLogMapper declareLogMapper;

    /**
     * 处理用户 - 当前申报状态，结果 以及开关
     */
    @Override
    public void updateDeclareInfo() {
        Integer startTime = sysParamService.getCurrentDeclareStartTime();
        Map<String, Object> params = new HashMap<>();
        params.put("istatus", EnumIstatus.OK.getKey());
        params.put("role", 1);
        List<AdminDO> adminDOList = adminMapper.selectByMap(params);
        if (!CollectionUtils.isEmpty(adminDOList)) {
            for (AdminDO adminDO : adminDOList) {
                try {
                   Declare declare = declareService.getCurrentDeclare(adminDO.getUserId(), startTime);
                   if (Objects.nonNull(declare)) {
                       AdminDO updateUser = new AdminDO();
                       updateUser.setUserId(adminDO.getUserId());
                       updateUser.setCurrentRoundDeclare(1);
                       updateUser.setCurrentRoundDeclareStatus(declare.getDeclareStatus());
                       //如果申报通过，关闭用户的申报开关
                       if (declare.getDeclareStatus() == 1) {
                           updateUser.setCurrentRoundDeclareSwitch(0);
                       }
                       adminMapper.updateByPrimaryKeySelective(updateUser);
                       log.info("updateDeclareInfo userId:{},params:{}", adminDO.getUserId(), JSONObject.toJSONString(updateUser));
                   } else {
                       log.info("updateDeclareInfo userId:{} startTime:{}, 暂无申报", adminDO.getUserId(), startTime);
                   }
                } catch (Exception e) {
                    log.error("updateDeclareInfo userId:{} error:{}", adminDO.getUserId(), e);
                }
            }
        }
    }

    @Transactional
    @Override
    public void delDeclareUser(Integer userId) {
       AdminDO adminDO = adminMapper.selectByPrimaryKey(userId);
       if (Objects.isNull(adminDO)) {
           throw new BizException(-1, "用户不存在或已删除!");
       }
       adminMapper.deleteByPrimaryKey(adminDO.getUserId());
       log.info("delDeclareUser 删除申报用户 success userId:{}", userId);
       //删除对应的申报信息
        Map<String,Object> params = new HashMap<>();
        params.put("createId", userId);
        List<Declare> declareList = declareMapper.selectByMap(params);
        if (!CollectionUtils.isEmpty(declareList)) {
            for (Declare declare : declareList) {
                //申报主信息
                declareMapper.deleteByPrimaryKey(declare.getId());
                //基础表
                declareBasicMapper.deleteByDeclareId(declare.getId());
                institutionMapper.deleteByDeclareId(declare.getId());
                disasterPlanMapper.deleteByDeclareId(declare.getId());
                disasterFacilitiesMapper.deleteByDeclareId(declare.getId());
                declareScienceMapper.deleteByDeclareId(declare.getId());
                classicCaseMapper.deleteByDeclareId(declare.getId());
                weatherBureauMapper.deleteByDeclareId(declare.getId());
                upgradeMapper.deleteByDeclareId(declare.getId());
                //附件表
                attachmentMapper.deleteByDeclareId(declare.getId());
                //日志表
                declareLogMapper.deleteByDeclareId(declare.getId());
            }
        }
    }
}
